<?php
/**
 * Created by PhpStorm.
 * User: Da Xiong
 * Date: 2019/2/21
 * Time: 13:46
 */

namespace App\HttpController\Admin;

use App\Model\AppletsModel;
use App\Utility\Cache;

/**
 * 小程序后台配置管理
 * Class AppletController
 * @package App\HttpController\Admin
 */
class AppletController extends AuthController
{

    /**
     * 获取列表
     */
    public function index(){
        $request = $this->request();
        $page = (int)$request->getRequestParam('page')??1;
        $params = $request->getRequestParam();
        $appletModel = new AppletsModel();
        $list = $appletModel->where(function ($query) use($params){
            if($params['kwd']){
                $params['kwd'] = trim($params['kwd']);
                $query->where('name','%'.$params['kwd'].'%','like')->whereOr('title','%'.$params['kwd'].'%','like')
                    ->whereOr('account','%'.$params['kwd'].'%','like');
            }
//            if($params['type']){
//                $query->where('applet_type',$params['type']);
//            }
        })
//      ->orderBy('id')
        ->paginate($page);
        return $this->writeJson(0,$list,'ok');
    }

    /**
     * 获取所有小程序
     */
    public function list(){
        $appletModel = new AppletsModel();
        $list = $appletModel->get();
        return $this->writeJson(0,$list,'ok');
    }

    /**
     * 新增/更新
     */
    public function update(){
        $request = $this->request();
        $params = $request->getRequestParam();
        if(!isset($params['name']) || !isset($params['account'])){
            return $this->writeJson(1,null,'参数错误！');
        }
        $appletModel = new AppletsModel();
        $cache = new Cache();
        if($params['id']){
            //更新
            if($appletModel->where('id',$params['id'],'<>','AND (')
                ->where('name',$params['name'])
                ->whereOr('account',$params['account'].')')->first()){
                return $this->writeJson(1,NULL,'name或邮箱已存在！');
            }
            $idInfo = $appletModel->find($params['id']);
            if(!$idInfo){
                return $this->writeJson(1,null,'更新失败，数据不存在！');
            }
            if($appletModel->update($params)){
                $cache_data = [
                    'appid'  => $params['appid'],
                    'secret' => $params['secret'],
                    'token'  => $params['token'],
                    'aeskey' => $params['aeskey'],
                ];
                $cache->set('config_'.$params['name'],$cache_data);
                return $this->writeJson(0,null,'更新成功');
            }
        }else{
            //新增
            if($appletModel->where('name',$params['name'],'=','OR')
                ->where('account',$params['account'],'=','OR')
                ->first()){
                return $this->writeJson(1,null,'name或邮箱账号已存在！');
            }
            $idInfo = $appletModel->create($params);
            if($idInfo){
                $cache_data = [
                    'appid'  => $params['appid'],
                    'secret' => $params['secret'],
                    'token'  => $params['token'],
                    'aeskey' => $params['aeskey'],
                ];
                $cache->set('config_'.$params['name'],$cache_data);
                return $this->writeJson(0,null,'新增成功');
            }
        }
        return $this->writeJson(1,null,'系统出错了');
    }


    /**
     * 删除
     * @return bool
     * @throws \Throwable
     */
    public function destroy(){
        $request = $this->request();
        $id = $request->getRequestParam('id');
        $appletModel = new AppletsModel();
        $cache = new Cache();
        $info = $appletModel->find($id);
        $cache->delete('config_'.$info['name']);
        $appletModel->destroy($id);
        return $this->writeJson(0);
    }

}
